<?php
/**
 * 小邮筒订单
 */
namespace soa\minpost\order\v1;

use soa\client;

//字段对应关系
class keyCorr  extends client
{
	private static $_keyCorr = [
		"kdhelp" => [
			"id" => "order_id",//订单号

			"waybill_no" => "waybill",//运单号
			"brand" => "brand",// 快递品牌

			"shipper_name" => "send_name",//发件人姓名
			"shipper_mobile" => "send_mobile",//发件人手机号
			"shipper_tel" => "send_tel",//发件人座机号
			"shipper_province" => "send_province",//发件人省
			"shipper_city" => "send_city",//发件人市
			"shipper_district" => "send_district",//发件人区县
			"shipper_address" => "send_address",//发件人地址
			"shipper_area_id" => "send_area_id",//发件人区域id

			"shipping_name" => "receive_name",//收件人姓名
			"shipping_mobile" => "receive_mobile",//收件人手机号
			"shipping_tel" => "receive_tel",//收件人座机号
			"shipping_province" => "receive_province",//收件人省
			"shipping_city" => "receive_city",//收件人市
			"shipping_district" => "receive_district",//收件人区县
			"shipping_address" => "receive_address",//收件人地址
			"shipping_area_id" => "receive_area_id",//收件人区域id

			"package_info" => "goods_name",//物品类型，或名称
			"package_note" => "goods_remark",//物品备注
			"package_weight" => "goods_weight",//包裹重量
			"charging_weight" => "goods_weight",//包裹重量

			"channel" => "source",//订单来源

			"certificate_path" => "proof_image",//底单图片

			"characters&orgCode" => "other_info&route_info&org_code",//sto->STO,第三方品牌编码
			"characters&mark" => "other_info&route_info&mark",//大字（大头笔）
			"characters&package" => "other_info&route_info&package",//集包地
			"characters&csCode" => "other_info&route_info&cs_code",//分拣码
			"characters&code1" => "other_info&route_info&code1",//一段码
			"characters&code2" => "other_info&route_info&code2",//二段码
			"characters&code3" => "other_info&route_info&code3",//三段码

			"is_print" => "print_status",//打印状态
		],
	];

	private static $_keySeg = "&";

	public static function convert($info,$orderPlatform = "kdhelp",$reverse = false){
		if(empty($info) || !is_array($info)){
			return [];
		}

		$keyArr = isset(self::$_keyCorr[$orderPlatform]) && !empty(self::$_keyCorr[$orderPlatform]) ? self::$_keyCorr[$orderPlatform] : [];

		if(empty($keyArr)){
			return [];
		}

		if($reverse){
			$keyArr = array_flip($keyArr);
		}

		$convertInfo = [];
		foreach($keyArr as $k => $v){
			$value = null;
			if(strpos($k,self::$_keySeg) !== false){
				$kArr = explode(self::$_keySeg,$k);
				if(!empty($kArr) && is_array($kArr)){
					foreach($kArr as $sk => $sv){
						if(0 == $sk){
							if(isset($info[$sv])){
								$value = $info[$sv];
							}else{
								$value = null;
								break;
							}
						}else{
							if(isset($value[$sv])){
								$value = $value[$sv];
							}else{
								$value = null;
								break;
							}
						}
					}
				}
			}else{
				if(isset($info[$k])){
					$value = $info[$k];
				}
			}

			if(strpos($v,self::$_keySeg) !== false){
				$vArr = explode(self::$_keySeg,$v);

				if(!empty($vArr) && is_array($vArr)){
					$ret = null;
					$saveValueArr = [];
					foreach($vArr as $sk => $sv){
						if(0 == $sk){
							if(isset($convertInfo[$sv])){
								$saveValueArr[$sv] = $convertInfo[$sv];
							}else{
								$saveValueArr[$sv] = [];
							}
						}else{
							if(!empty($upSv) && isset($saveValueArr[$upSv][$sv])){
								$saveValueArr[$sv] = $saveValueArr[$upSv][$sv];
							}
						}
						$upSv = $sv;
					}

					$vArr = array_reverse($vArr);
					$mergeValueArr = [];
					foreach($vArr as $sk => $sv){
						if($sk == count($vArr) - 1){
							$convertInfo[$sv] = array_merge($convertInfo[$sv] ? : [],$mergeValueArr);
						}else{
							if(0 == $sk){
								$mergeValueArr[$sv] = $value;
							}else{
								$mergeValueArr[$sv] = array_merge($saveValueArr[$sv] ? : [],$mergeValueArr);
							}
						}
					}
				}

			}else{
				$convertInfo[$v] = $value;
			}
		}

		return $convertInfo;
	}

	//第三方订单信息下单
	public function thirdOrderPlace($thirdOrderInfo,$orderPlatform = "kdhelp",$extraInfo = []){
		$code = 9000;
		if(empty($thirdOrderInfo) || !is_array($thirdOrderInfo) || empty($orderPlatform) || !is_string($orderPlatform)){
			$this->_code = $code;$this->_msg = "第三方订单信息错误";
			return [];
		}

		$code++;
		if(empty($extraInfo) || !is_array($extraInfo) || !isset($extraInfo["uid"]) || empty($extraInfo["uid"]) || !isset($extraInfo["platform"]) || empty($extraInfo["platform"])
			|| !isset($extraInfo["agent_guid"]) || empty($extraInfo["agent_guid"])){
			$this->_code = $code;$this->_msg = "下单用户信息缺失";
			return $extraInfo;
		}

		$orderInfo = $this->convert($thirdOrderInfo,$orderPlatform);
		$code++;
		if(empty($orderInfo) || !isset($orderInfo["order_id"]) || empty($orderInfo["order_id"])){
			$this->_code = $code;$this->_msg = "第三方订单信息转换错误";
			return [];
		}
		$thirdOrderId = $orderInfo["order_id"];
		unset($orderInfo["order_id"]);
		$orderInfo["third_order_id"] = $thirdOrderId;
		$orderInfo["third_platform"] = $orderPlatform;
		$orderInfo["uid"] = $extraInfo["uid"];
		$orderInfo["platform"] = $extraInfo["platform"];
		$orderInfo["agent_guid"] = $extraInfo["agent_guid"];

		//过滤掉无意义的数值
		$filterOrderInfo = [];
		foreach($orderInfo as $key => $value){
			if(null !== $value && "" !== $value){
				$filterOrderInfo[$key] = trim($value);
			}
		}

		$orderConnObj = new \soa\minpost\order\v1\conn();
		$orderConnRet = $orderConnObj->getOrderByThird($thirdOrderId,$orderPlatform);

		$orderOperateObj = new \soa\minpost\order\v1\orderOperate();
		if(empty($orderConnRet)){
			$ret = $orderOperateObj->orderPlace($filterOrderInfo);
		}else{
			$ret = $orderOperateObj->updateOrder(["order_id" => $orderConnRet["order_id"]],$filterOrderInfo);
		}
		$this->_code = $orderOperateObj->getCode();$this->_msg = $orderOperateObj->getMsg();
		return $ret;
	}
}